// pages/reviewDetail/reviewDetail.js
let receipt = null  // 批改所属签收的对象
let request = require('../../operation/operation.js')
let bxt = require('../../utils/bxt.js')
let noticeModel = require('../../models/notice.js')

let reviewContent = ''
let reviewFlowerCount = 0
let receiptReviewResult = noticeModel.REVIEW_RESULT.PASS // 当前签收对象批改结果

let reviewModalTemplate = require('../../template/reviewModal/reviewModal.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    review:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    receipt = getApp().globalData.param.receipt
    this.setData({
      review:receipt.review
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    receipt = null
    reviewFlowerCount = 0 
  },

  bindKeyInput: function (event) {
    reviewContent = event.detail.value
  },

  onReview:function() {
    let that = this

    wx.showActionSheet({
      itemList: ['通过', '不通过'],
      success: function (res) {
        receiptReviewResult = res.tapIndex + 1
        if (noticeModel.REVIEW_RESULT.FAIL == receiptReviewResult) {
          reviewFlowerCount = 0
        }else {
          if (receipt.review.flower) {
            reviewFlowerCount = receipt.review.flower.count
          }else {
            reviewFlowerCount = 0
          }
          
        }
        reviewContent = receipt.review.content
        reviewModalTemplate.show(that)
        reviewModalTemplate.initRate(receiptReviewResult, reviewFlowerCount,reviewContent)
      }
    })
  },

  onRate:function(event) {
    reviewFlowerCount = parseInt(event.currentTarget.id)
    reviewModalTemplate.setFlowerCount(event.currentTarget.id)
  },

  onConfirm: function () {
    let that = this

    reviewModalTemplate.updateReview(receipt.review.sid, receiptReviewResult, reviewContent, reviewFlowerCount).then(res => {
      reviewModalTemplate.cancel()
        let review = res
      if (receipt.review.result != receiptReviewResult) {
          let notice
          if (receiptReviewResult == noticeModel.REVIEW_RESULT.FAIL) {
              notice = noticeModel.updateNoticeReceiptStatus(getApp().globalData.param.notice,1)
              getApp().notificationCenter.post(bxt.REVIEW_FAIL_MESSAGE, { notice,review })
          }else {
              notice = noticeModel.updateNoticeReceiptStatus(getApp().globalData.param.notice,-1)
              getApp().notificationCenter.post(bxt.REVIEW_PASS_MESSAGE, { notice,review })
          }
      }else {
          getApp().notificationCenter.post(bxt.REVIEW_UPDATE_MESSAGE, { review })
      }

      getApp().autoBack('批改完成',1)
    })
  },

  onCancel: function () {
    reviewModalTemplate.cancel()
  },

  /**
   * 点击删除事件处理函数
   */
  onDelReview:function() {
    wx.showModal({
      title: '删除批改',
      content: '确定删除批改？',
      showCancel: true,      
      success: function(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '请稍候',
            mask: true            
          })
          request.deleteRequest('/reviews/' + receipt.review.sid, {}, true)
            .then(res => {
              wx.hideLoading()
              var data = res.data;
              if (request.SUCCESSED == data.status) {
                let notice = noticeModel.updateNoticeReceiptStatus(getApp().globalData.param.notice, -1)
                getApp().notificationCenter.post(bxt.DELETE_REVIEW_MESSAGE, { notice, receipt})

                getApp().autoBack('删除成功',1)                                
              } else {
                wx.showToast({
                  title: data.msg,
                  icon: 'none'
                })
              }
            })
            .catch(e => {
              wx.hideLoading()
              console.log(e)
            })
        }        
      }
    })
  }
})